package com.eling.elcms.travel.dao.hibernate;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.eling.elcms.core.dao.hibernate.GenericDaoHibernate;
import com.eling.elcms.travel.dao.IRoomPersonalInfoDao;
import com.eling.elcms.travel.model.RoomPersonalInfo;


@Repository
public class RoomPersonalInfoDaoHibernate  extends GenericDaoHibernate<RoomPersonalInfo, Long> implements IRoomPersonalInfoDao{

	public RoomPersonalInfoDaoHibernate() {
		super(RoomPersonalInfo.class);
	}

	@Override
	public RoomPersonalInfo saveBySql(RoomPersonalInfo roomPersonalInfo) {
		StringBuffer sql = new StringBuffer();
		//由于出现了表名大小写问题。写sql保存
		if(roomPersonalInfo.getPkRoomPersonalInfo() != null){
			sql.append(" UPDATE `travel_roompersonalInfo`  ");
			sql.append(" SET  ");
			sql.append(" `pkRoomPersonalInfo` = '"+roomPersonalInfo.getPkRoomPersonalInfo()+"',   ");
			
			sql.append("  `changeRoomFee` = "+roomPersonalInfo.getChangeRoomFee()+",          ");
			sql.append("  `checkIn` = '"+roomPersonalInfo.getCheckIn()+"',                ");
			sql.append("  `descript` = "+roomPersonalInfo.getDescript()+",               ");
			sql.append("  `version` = '"+roomPersonalInfo.getVersion()+"',                 ");
			sql.append("  `pkCIImplement` = '"+roomPersonalInfo.getCheckInImplement().getPkCIImplement()+"',          ");
			if(roomPersonalInfo.getCheckOutSettlement()!=null){
				sql.append("  `pkCheckOutSettlement` = "+roomPersonalInfo.getCheckOutSettlement().getPkCheckOutSettlement()+",   ");
			}
			sql.append("  `pkConsumeCard` = "+roomPersonalInfo.getConsumeCard()+",          ");
			sql.append("  `pkNonMemberRegister` = '"+roomPersonalInfo.getNonMemberRegister().getPkNonMemberRegister()+"',    ");
			if(roomPersonalInfo.getNonMemberRegister().getSignUpRegister().getPersonalInfo() != null &&roomPersonalInfo.getNonMemberRegister().getSignUpRegister().getPersonalInfo().getPkPersonalInfo() != null){
				sql.append("  `pkPersonalInfo` = '"+roomPersonalInfo.getNonMemberRegister().getSignUpRegister().getPersonalInfo().getPkPersonalInfo()+"',     ");
			}
			
			sql.append("  `pkRoomRegister` = '"+roomPersonalInfo.getRoomRegister().getPkRoomRegister()+"',         ");
			if(roomPersonalInfo.getSignUpRegister() != null){
				sql.append("  `pkSignUpRegister` = "+roomPersonalInfo.getSignUpRegister().getPkSignUpRegister()+"        ");
			}
			
			sql.append(" WHERE                             ");
			sql.append(" 	(`pkRoomPersonalInfo` = '"+roomPersonalInfo.getPkRoomPersonalInfo()+"');  ");
			getSession().createNativeQuery(sql.toString());
		}else {
			sql.append(" INSERT INTO `travel_roompersonalInfo` (   ");
			sql.append(" 	`changeRoomFee`,                                 ");
			sql.append(" 	`checkIn`,                                       ");
			sql.append(" 	`descript`,                                      ");
			sql.append(" 	`version`,                                       ");
			sql.append(" 	`pkCIImplement`,                                 ");
			if(roomPersonalInfo.getCheckOutSettlement()!=null){
				sql.append(" 	`pkCheckOutSettlement`,                          ");
			}
			sql.append(" 	`pkConsumeCard`,                                 ");
			sql.append(" 	`pkNonMemberRegister`,                           ");
			sql.append(" 	`pkPersonalInfo`,                                ");
			sql.append(" 	`pkRoomRegister`                                ");
			if(roomPersonalInfo.getSignUpRegister() != null){
				sql.append(" 	,`pkSignUpRegister`                               ");
			}
			sql.append(" )                                                   ");
			sql.append(" VALUES                                              ");
			sql.append(" 	(                                                ");
			sql.append(" 		"+roomPersonalInfo.getChangeRoomFee()+",                                        ");
			sql.append(" 		'"+roomPersonalInfo.getCheckIn()+"',                                       ");
			sql.append(" 		"+roomPersonalInfo.getDescript()+",                                        ");
			sql.append(" 		'0',                                         ");
			sql.append(" 		'"+roomPersonalInfo.getCheckInImplement().getPkCIImplement()+"',                       ");
			if(roomPersonalInfo.getCheckOutSettlement()!=null){
				sql.append("  "+roomPersonalInfo.getCheckOutSettlement().getPkCheckOutSettlement()+",   ");
			}
			sql.append(" 		"+roomPersonalInfo.getConsumeCard()+",                                        ");
			sql.append(" 		"+roomPersonalInfo.getNonMemberRegister().getPkNonMemberRegister()+",                                        ");
			sql.append(" 		"+roomPersonalInfo.getPersonalInfo().getPkPersonalInfo()+",                        ");
			sql.append(" 		'"+roomPersonalInfo.getRoomRegister().getPkRoomRegister()+"',                                        ");
			if(roomPersonalInfo.getSignUpRegister() != null){
				sql.append(" 		"+roomPersonalInfo.getSignUpRegister().getPkSignUpRegister()+"                  ");
			}
			sql.append(" 	)                                                 ");
			getSession().createNativeQuery(sql.toString());
		}
		
		if(roomPersonalInfo.getPkRoomPersonalInfo() != null){
			roomPersonalInfo = get(roomPersonalInfo.getPkRoomPersonalInfo());
		}
		return roomPersonalInfo;
	}
	
}
